package org.sam.swing.table;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import org.apache.commons.lang3.StringUtils;
import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
import org.jdesktop.swingx.table.TableColumnExt;
import org.sam.swing.table.renderer.JSTableRowNumberRenderer;

/**
 * 扩展的TableColumnModel
 * 
 * @author sam
 *
 */
public class JSTableColumnModel extends DefaultTableColumnModelExt {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3443304855739066731L;

	/**
	 * 获取第一个可编辑列
	 * 
	 * @return
	 */
	public int getFirstEditColumnModelIndex() {
		List<TableColumn> columns = this.getColumns(true);
		if (columns == null || columns.isEmpty())
			return -1;

		for (TableColumn coloumn : columns) {
			JSTableColumn col = (JSTableColumn) coloumn;
			if (col.isEditable())
				return coloumn.getModelIndex();
		}
		return -1;
	}

	/**
	 * 获取可视列（去掉原始列和隐藏列）
	 * 
	 * @return
	 */
	public List<TableColumnExt> getVisibleColumns() {
		List<TableColumnExt> result = new ArrayList<>();

		for (int i = 0, iCount = this.getColumnCount(); i < iCount; i++) {
			TableColumnExt columnExt = this.getColumnExt(i);
			if (columnExt.isVisible() && !JSTableColumn.COLUMN_ORIGINAL.equals(columnExt.getIdentifier())) {
				result.add(columnExt);
			}
		}

		return result;
	}

	/**
	 * 获取可视列（去掉原始列,隐藏列,行号列）
	 * 
	 * @return
	 */
	public List<TableColumnExt> getVisibleDataColumns() {
		List<TableColumnExt> result = new ArrayList<>();

		for (int i = 0, iCount = this.getColumnCount(); i < iCount; i++) {
			TableColumnExt columnExt = this.getColumnExt(i);
			if (columnExt.isVisible() && !JSTableColumn.COLUMN_ORIGINAL.equals(columnExt.getIdentifier())) {

				TableCellRenderer cellRenderer = columnExt.getCellRenderer();
				if (cellRenderer != null && cellRenderer instanceof JSTableRowNumberRenderer)
					continue;

				result.add(columnExt);
			}
		}
		return result;
	}

	/**
	 * 获取列对应的真实可编辑列的从0开始的下标
	 * 
	 * @param field
	 * @return -1 未找到
	 */
	public int getDataIdentifierIndex(String field) {
		List<TableColumnExt> columns = getVisibleDataColumns();
		if (columns == null || columns.isEmpty() || StringUtils.isEmpty(field))
			return -1;

		for (int i = 0; i < columns.size(); i++) {
			TableColumnExt col = columns.get(i);
			if (field.equals(col.getIdentifier()))
				return i;
		}
		
		return -1;
	}

	/**
	 * 获取列序号和名称对应的关系
	 * 
	 * @return key modelIndex ,
	 */
	public Map<Integer, String> getColumnNamesMapWithIndex() {
		Map<Integer, String> colNames = new LinkedHashMap<>();

		for (int i = 0, iCount = this.getColumnCount(); i < iCount; i++) {
			TableColumnExt column = this.getColumnExt(i);
			if (column == null || JSTableColumn.COLUMN_ORIGINAL.equals(column.getIdentifier()))
				continue;

			if (column.getIdentifier() == null || column.getIdentifier().toString().length() <= 0
					|| !column.isVisible())
				continue;

			colNames.put(column.getModelIndex(), column.getTitle());
		}

		return colNames;
	}
}
